<?php

namespace App\Http\Controllers\Admin\miniapp;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\miniapp\Goods;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $items = Goods::latest('is_sale')->latest('created_at')->paginate(10);
        return $this->successful($items);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->successful(Goods::create($request->all()));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\miniapp\Goods  $good
     * @return \Illuminate\Http\Response
     */
    public function show(Goods $good)
    {
        return $this->successful($good);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  \App\Models\miniapp\Goods  $good
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Goods $good)
    {
        $good->update($request->all());
        return $this->successful();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\miniapp\Goods  $good
     * @return \Illuminate\Http\Response
     */
    public function destroy(Goods $good)
    {
        $good->delete();
        return $this->successful();
    }

    public function batchDelete()
    {
        $ids = request()->post('id');
        if ($ids) {
            Goods::destroy($ids);
        }
        return $this->successful($ids, '删除成功');
    }
}
